package com.domor.xxx.system.controller.system;

import com.domor.xxx.common.annotation.Log;
import com.domor.xxx.common.core.controller.BaseController;
import com.domor.xxx.common.core.domain.AjaxResult;
import com.domor.xxx.common.core.domain.TreeEntity;
import com.domor.xxx.framework.shiro.util.ShiroUtils;
import com.domor.xxx.system.domain.SysMenu;
import com.domor.xxx.system.service.SysMenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <h3>菜单管理控制层</h3>
 *
 * @author liyuyang
 * @date 2020/4/23
 **/
@Controller
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {

    private String prefix = "system/menu";

    @Autowired
    private SysMenuService menuService;

    @GetMapping()
    public String menu() {
        return prefix + "/index";
    }

    @PostMapping("/list")
    @ResponseBody
    public List<SysMenu> list() {
        List<SysMenu> menuList = menuService.selectMenuList();
        return menuList;
    }

    /**
     * 删除菜单
     */
    @Log(title = "菜单管理")
    @RequiresPermissions("system:menu:delete")
    @PostMapping("/delete/{menuId}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("menuId") Long menuId) {
        if (menuService.selectCountMenuByParentId(menuId) > 0) {
            return AjaxResult.warn("存在子菜单,不允许删除");
        }
        if (menuService.selectCountRoleMenuByMenuId(menuId) > 0) {
            return AjaxResult.warn("菜单已分配,不允许删除");
        }
        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(menuService.deleteMenuById(menuId));
    }

    /**
     * 新增
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap) {
        SysMenu menu = new SysMenu();
        if (0L != parentId) {
            SysMenu pmenu = menuService.selectMenuById(parentId);
            menu.setMenuType(pmenu.getMenuType() + 1);
            menu.setParentId(parentId);
        } else {
            menu.setMenuType(1);
            menu.setParentId(0L);
        }
        mmap.put("menu", menu);
        return prefix + "/add";
    }

    /**
     * 新增保存菜单
     */
    @Log(title = "菜单管理")
    @RequiresPermissions("system:menu:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysMenu menu) {
        if (menuService.checkMenuNameUnique(menu)) {
            return error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        menu.setCreateBy(ShiroUtils.getLoginName());
        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(menuService.insertMenu(menu));
    }

    /**
     * 修改菜单
     */
    @GetMapping("/edit/{menuId}")
    public String edit(@PathVariable("menuId") Long menuId, ModelMap mmap) {
        mmap.put("menu", menuService.selectMenuById(menuId));
        return prefix + "/edit";
    }

    /**
     * 修改保存菜单
     */
    @Log(title = "菜单管理")
    @RequiresPermissions("system:menu:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysMenu menu) {
        if (menuService.checkMenuNameUnique(menu)) {
            return error("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        menu.setUpdateBy(ShiroUtils.getLoginName());
        ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(menuService.updateMenu(menu));
    }

    /**
     * 选择菜单图标
     */
    @GetMapping("/icon")
    public String icon() {
        return prefix + "/icon";
    }

    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree/{menuId}")
    public String selectMenuTree(@PathVariable("menuId") Long menuId, ModelMap mmap) {
        mmap.put("menu", menuService.selectMenuById(menuId));
        return prefix + "/tree";
    }

    /**
     * 加载所有菜单列表树
     */
    @PostMapping("/menuTree")
    @ResponseBody
    public List<TreeEntity> menuTree() {
        List<TreeEntity> children = menuService.menuTree();
        for (TreeEntity t : children) {
            t.setState("closed");
        }
        TreeEntity tree = new TreeEntity(0,"主目录");
        tree.setChildren(children);
        return Arrays.asList(tree);
    }

    /**
     * 加载角色菜单列表树
     */
    @PostMapping("/roleMenuTree")
    @ResponseBody
    public List<TreeEntity> roleMenuTree(Long roleId) {
        return menuService.roleMenuTree(roleId);
    }

}